//
//  ZQAdminSetting.m
//  jianzhik
//
//  Created by huami on 2018/8/28.
//  Copyright © 2018年 WKWebView. All rights reserved.
//
#import "ZQAdminCell.h"
#import "ZQAdminSetting.h"
#import "ZQAddAdminPage.h"
#import "ZQAdminModel.h"
#import <MJExtension/MJExtension.h>
#import "UIBarButtonItem+item.h"
#import "BRPickerView.h"

@interface ZQAdminSetting ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, copy) NSArray *textArr;
/** listArr */
@property (nonatomic, copy) NSMutableArray <ZQAdminModel *>*listMuArr;

/** listNomalArr */
@property (nonatomic, strong) NSMutableArray *listNomalArr;

/** listForbiddenArr */
@property (nonatomic, strong) NSMutableArray *listForbiddenArr;


/** status */
@property (nonatomic, strong) NSNumber *status;

/** rightItemTitle */
@property (nonatomic, copy) NSString *rightItemTitle;
@end

@implementation ZQAdminSetting

- (NSNumber *)status
{
    if (!_status) {
        _status = [[NSNumber alloc] init];
    }
    return _status;
}

- (NSMutableArray *)listNomalArr
{
    if (!_listNomalArr) {
        _listNomalArr = [[NSMutableArray alloc] init];
    }
    return _listNomalArr;
}

- (NSMutableArray *)listForbiddenArr
{
    if (!_listForbiddenArr) {
        _listForbiddenArr = [[NSMutableArray alloc] init];
    }
    return _listForbiddenArr;
}

- (NSMutableArray *)listMuArr
{
    if (!_listMuArr) {
        _listMuArr = [[NSMutableArray alloc] init];
    }
    return _listMuArr;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [self AfnetworkRequest:parameters];
}

- (NSNumber *)valueWithDict:(NSString *)str
{
    NSDictionary *dict = @{
                           @"正常状态" : @10,
                           @"禁用状态" : @20
                           };
    return dict[str];
}

- (void)setUpNavBar
{
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem backItemWithImage:nil hightImage:nil target:self action:@selector(editClick) title:@"正常状态"];
}

- (void)editClick
{
    
    NSArray *dataSource = @[@"正常状态",@"禁用状态"];
    [BRStringPickerView showStringPickerWithTitle:@"状态" dataSource:dataSource defaultSelValue:self.rightItemTitle isAutoSelect:NO themeColor:kTinColor resultBlock:^(id selectValue) {
        self.rightItemTitle = selectValue;
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem backItemWithImage:nil hightImage:nil target:self action:@selector(editClick) title:selectValue];
        self.status = [selectValue isEqualToString:@"正常状态"] ? @10 : @20;
        [self.tableView reloadData];
//        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//        parameters[@"Status"] = [self valueWithDict:selectValue];
//        [self AfnetworkRequest:parameters];
    } cancelBlock:^{
        NSLog(@"点击了背景视图或取消按钮");
    }];
}

- (void)AfnetworkRequest:(NSMutableDictionary *)parameters
{
    if (parameters.count == 0) {
        parameters = nil;
    }
    [ZQAFHelper afnetworkRequestWithGetURL:@"/v1/user/listuser" parameters:parameters view:self.view finishedBlcok:^(NSDictionary *dict) {
        //        dict[Data]
        NSArray *arr = dict[@"Data"];
        if (arr.count == 0) {
            self.listMuArr = arr.mutableCopy;
            self.listForbiddenArr = arr.mutableCopy;
            self.listNomalArr = arr.mutableCopy;
            [UIHelper showTipsWithText:@"查无数据" inView:self.view];
            return ;
        } else{
            self.listMuArr = [ZQAdminModel mj_objectArrayWithKeyValuesArray:arr];
            [self.listMuArr enumerateObjectsUsingBlock:^(ZQAdminModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.Status == 10) {
                    [self.listNomalArr addObject:obj];
                } else if (obj.Status == 20) {
                    [self.listForbiddenArr addObject:obj];
                }
            }];
            [UIHelper dismissLoadingInView:self.view];
        }
        [self.tableView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpNavBar];
    [self.footButton setTitle:@"添加管理员" forState:UIControlStateNormal];
    [self.footButton addTarget:self action:@selector(addAdmin) forControlEvents:UIControlEventTouchUpInside];
    [self registerCell:[ZQAdminCell class]];
    self.rightItemTitle = @"正常";
    self.status = @10;
}

- (void)addAdmin
{
   ZQAddAdminPage *vc =   [ZQAddAdminPage new];
    vc.title = @"管理员设置";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.status intValue] == 10 ? self.listNomalArr.count : self.listForbiddenArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZQAdminModel *model = [self.status  isEqual: @10] ? self.listNomalArr[indexPath.row] : self.listForbiddenArr[indexPath.row];
    ZQAdminCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID  forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.adModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 67.5;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.listMuArr = [NSMutableArray array];
    [self.listForbiddenArr removeAllObjects];
    [self.listNomalArr removeAllObjects];

    
}
@end
